package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


/**
 * Servlet implementation class Listado
 */
@WebServlet("/ListadoEliminar") 
public class ListadoEliminar extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /** 
     * @see HttpServlet#HttpServlet()
     */
    public ListadoEliminar() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	
	
	
	protected void finalize(){
		
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		
		String icono="class=\"ui-icon ui-icon-close\""; 
		
		String query=request.getParameter("query");
		
		PrintWriter out = response.getWriter();
		response.setContentType("application/json");
		
		Map<String, ArrayList> strMap = new HashMap<String, ArrayList>();
		ArrayList aList1 = new ArrayList();
		ArrayList aList2 = new ArrayList();
		ArrayList aList3 = new ArrayList();
				
		if(query.length()>0){
			aList1.add("<INPUT TYPE=RADIO NAME=\"prueba\" VALUE=\"1\" onclick=eraseform() />");
			aList1.add("Juny");
			aList1.add("Julio Albert");
		}else{
			aList1.add("<INPUT TYPE=RADIO NAME=\"prueba\" VALUE=\"1\" onclick=eraseform() />");
			aList1.add("Eyeshield21");
			aList1.add("Olivia Wilde");
		}
				
		aList2.add("<INPUT TYPE=RADIO NAME=\"prueba\" VALUE=\"1\" onclick=eraseform() />");
		aList2.add("Hulk");
		aList2.add("Jennifer Connelly");

		
		aList3.add("<INPUT TYPE=RADIO NAME=\"prueba\" VALUE=\"1\" onclick=eraseform() />");
		aList3.add("Albert");
		aList3.add("Albert Einstein");

			
		if(query.length()>0){ 
			strMap.put("1",aList2);
			strMap.put("2",aList3);
			strMap.put("3",aList1);
		}else{
			strMap.put("1",aList2);
			strMap.put("2",aList1);
			strMap.put("3",aList3);
			}
			 
		JSONArray jArray = new JSONArray();
		for(String key:strMap.keySet()) {
		JSONObject jsonObj = new JSONObject();
			try {
				jsonObj.put("id", key);
				jsonObj.put("cell", strMap.get(key));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		jArray.put(jsonObj);
		}

		JSONObject jObj = new JSONObject();
			try {
				jObj.put("page", 1);
				jObj.put("total", strMap.size());
				jObj.put("rows", jArray);
				if(query.length()>0){
					jObj.put("sortname", "id");
					jObj.put("sortorder", "asc");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		response.setContentType("application/json");
		out.println(jObj);
		out.close();
		
		
	}

}
